⚡️ Speed up function _find_type_node by 144% in PR #1199 (omni-java)#1250
Closed
codeflash-ai[bot] wants to merge 1 commit intoomni-javafrom
Closed
⚡️ Speed up function _find_type_node by 144% in PR #1199 (omni-java)#1250codeflash-ai[bot] wants to merge 1 commit intoomni-javafrom
_find_type_node by 144% in PR #1199 (omni-java)#1250codeflash-ai[bot] wants to merge 1 commit intoomni-javafrom
Conversation
The optimized code achieves a **143% speedup** (from 169μs to 69.3μs) through three key performance improvements:
**1. Eliminated Dictionary Reconstruction Overhead (25.3% of original time)**
The original code rebuilt the `type_declarations` dictionary on every recursive call (329 times in the profiler). By hoisting it to a module-level constant `TYPE_DECLARATIONS`, this overhead is completely eliminated. The profiler shows ~340μs spent creating this dict repeatedly in the original version.
**2. Replaced Recursion with Iterative Stack-Based DFS**
The original code made 319 recursive calls (`_find_type_node(child, type_name, source_bytes)` at 638μs). Each recursive call incurs Python function call overhead including frame creation, argument passing, and local variable setup. The optimized version uses an explicit stack to traverse the tree iteratively, eliminating this overhead entirely. This is especially impactful in the deep tree test case, which shows **211% speedup** (146μs → 47μs).
**3. Direct Byte Comparison Instead of UTF-8 Decoding**
The original code decoded byte slices to strings 12 times (`source_bytes[...].decode("utf8")` at 13μs). The optimized version encodes `type_name` to bytes once at function entry (10.7μs for 10 calls), then performs direct byte-to-byte comparison without any decoding. This is particularly effective for multibyte UTF-8 names, as shown in the UTF-8 test case with **25.1% speedup** (2.24μs → 1.79μs).
**Performance Analysis by Test Case:**
- Simple cases show modest improvements (0-2μs) due to lower overhead
- Nested/deep traversals show dramatic gains (e.g., 211% on 300-depth tree) where recursion elimination matters most
- UTF-8 handling improves 25% by avoiding repeated decode operations
- A few edge cases show minor regression (1-6% slower) due to stack manipulation overhead, but these are dwarfed by gains in realistic workloads
The optimization preserves exact behavior including traversal order (reversed children maintain left-to-right DFS), return types, and edge case handling while delivering significant runtime improvements especially for deep syntax trees—a common scenario when parsing Java source code.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
⚡️ This pull request contains optimizations for PR #1199
If you approve this dependent PR, these changes will be merged into the original PR branch
omni-java.📄 144% (1.44x) speedup for
_find_type_nodeincodeflash/languages/java/context.py⏱️ Runtime :
169 microseconds→69.3 microseconds(best of22runs)📝 Explanation and details
The optimized code achieves a 143% speedup (from 169μs to 69.3μs) through three key performance improvements:
1. Eliminated Dictionary Reconstruction Overhead (25.3% of original time)
The original code rebuilt the
type_declarationsdictionary on every recursive call (329 times in the profiler). By hoisting it to a module-level constantTYPE_DECLARATIONS, this overhead is completely eliminated. The profiler shows ~340μs spent creating this dict repeatedly in the original version.2. Replaced Recursion with Iterative Stack-Based DFS
The original code made 319 recursive calls (
_find_type_node(child, type_name, source_bytes)at 638μs). Each recursive call incurs Python function call overhead including frame creation, argument passing, and local variable setup. The optimized version uses an explicit stack to traverse the tree iteratively, eliminating this overhead entirely. This is especially impactful in the deep tree test case, which shows 211% speedup (146μs → 47μs).3. Direct Byte Comparison Instead of UTF-8 Decoding
The original code decoded byte slices to strings 12 times (
source_bytes[...].decode("utf8")at 13μs). The optimized version encodestype_nameto bytes once at function entry (10.7μs for 10 calls), then performs direct byte-to-byte comparison without any decoding. This is particularly effective for multibyte UTF-8 names, as shown in the UTF-8 test case with 25.1% speedup (2.24μs → 1.79μs).Performance Analysis by Test Case:
The optimization preserves exact behavior including traversal order (reversed children maintain left-to-right DFS), return types, and edge case handling while delivering significant runtime improvements especially for deep syntax trees—a common scenario when parsing Java source code.
✅ Correctness verification report:
🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-pr1199-2026-02-02T00.25.11and push.